//
//  HSReleaseInfoController.swift
//  home-school
//
//  Created by 张晓林 on 16/7/19.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSReleaseInfoController: UIViewController,UITextViewDelegate {
  
    // 标题输入框
    fileprivate var feedbackTitleF: UITextField!
    // 内容输入框
    fileprivate var feedbackContentV: UITextView!
    // 提示标签
    fileprivate var placeholderL: UILabel!
    // 清除按钮
    fileprivate var clearBtn: UIButton!
    
    // MARK: - UI相关
    override func viewDidLoad() {
        super.viewDidLoad()
        // 标题
        title = "动态"
        // 背景色
        view.backgroundColor = UIColor().colorWithHex(0xf0f0f0)
        
        // 添加设置UI
        setupUI()
    }
    
    // 添加设置UI
    fileprivate func setupUI() {
        // 标题输入框
        feedbackTitleF = UITextField(frame: CGRect(x: 0, y: 64, width: screenW, height: 44))
        feedbackTitleF.backgroundColor = UIColor.white
        feedbackTitleF.placeholder = "发布标题"
        feedbackTitleF.textAlignment = .center
        
        // 调整左边光标位置
        feedbackTitleF.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 3, height: 44))
        feedbackTitleF.leftViewMode = .always
        // 设置右边清楚按钮
        feedbackTitleF.clearButtonMode = .always
        view.addSubview(feedbackTitleF)
        
        // 内容输入框
        feedbackContentV = UITextView(frame: CGRect(x: 0, y: feedbackTitleF.frame.maxY+20, width: screenW, height: screenH*0.292))
        feedbackContentV.backgroundColor = UIColor.white
        // 设置显示内容位置偏移
        feedbackContentV.textContainerInset = UIEdgeInsetsMake(8, 0, 0, 23)
        feedbackContentV.delegate = self
        view.addSubview(feedbackContentV)
        
        // 添加提示标签
        placeholderL = UILabel(title: "说点什么吧...", color: UIColor(white: 0.75, alpha: 1.0), fontSize: 15)
        placeholderL.frame = CGRect(x: 4, y: 0, width: screenW-20, height: 30)
        feedbackContentV.addSubview(placeholderL)
        
        // 添加清除按钮
        clearBtn = UIButton(type: UIButtonType.system)
        clearBtn.isHidden = true // 默认隐藏
        clearBtn.frame = CGRect(x: screenW - 23, y: 8, width: 15, height: 15)
        clearBtn.setBackgroundImage(UIImage(named: "清除"), for: UIControlState())
        clearBtn.addTarget(self, action: #selector(clearBtnClicked), for: UIControlEvents.touchUpInside)
        feedbackContentV.addSubview(clearBtn)
        
        // 确定按钮
        let suerBtn = UIButton(type: UIButtonType.custom)
        suerBtn.frame = CGRect(x: 30, y: feedbackContentV.frame.maxY+20, width: screenW-60, height: 44)
        suerBtn.setTitle("发     送", for: UIControlState())
        suerBtn.setBackgroundImage(UIImage(named: "feedbackBtn_n"), for: UIControlState())
        suerBtn.setBackgroundImage(UIImage(named: "feedbackBtn_h"), for: UIControlState.highlighted)
        // 圆角
        suerBtn.layer.cornerRadius = 5
        suerBtn.layer.masksToBounds = true
        // 按钮监听方法
        suerBtn.addTarget(self, action: #selector(suerBtnClicked), for: UIControlEvents.touchUpInside)
        view.addSubview(suerBtn)
    }
    
    // MARK: - 按钮的监听回调方法
    // 清除按钮的监听回调方法
    @objc fileprivate func clearBtnClicked() {
        feedbackContentV.text = ""
        placeholderL.isHidden = false
        clearBtn.isHidden = true
    }
    
    // 提交按钮的监听回调方法
    @objc fileprivate func suerBtnClicked() {
        // 获得输入的标题和内容
        let title = feedbackTitleF.text
        let content = feedbackContentV.text
        
        if title == "" {
            HSAlertView.bottomAlertView("标题不能为空")
            return
        }
        
        if content == "" {
            HSAlertView.bottomAlertView("内容不能为空")
            return
        }
        // 退出编辑模式
        view.endEditing(true)
        // 守护用户key值
        guard let key = userInfo["key"] else {
            return
        }
        // 用户的输入内容
        let feedbackInfo = ["key":key ,"title":title!,"content":content!]
        // 获得发送接口
        let url = "ZH-schoolname-S-submitJXMessage"
        
        // 发送消息数据
        HSNetworkTools.shared.downloadData(url: url, parameters: feedbackInfo) { (res, err) in
            if err == nil {
                HSAlertView.bottomAlertView("消息发送成功")
            }else {
                HSAlertView.bottomAlertView("网络连接异常")
            }
        }

    }
    
    // 返回
    @objc fileprivate func backBtnClicked() {
        // 退出编辑模式
        view.endEditing(true)
        // 退出控制器
        dismiss(animated: true, completion: nil)
    }
    
    // MARK: - 文本输入的代理方法---处理提示文本的显示
    func textViewDidChange(_ textView: UITextView) {
        if textView.text != "" {
            placeholderL.isHidden = true
            clearBtn.isHidden = false
        }else {
            placeholderL.isHidden = false
            clearBtn.isHidden = true
        }
    }

}
